package Views.Toner;

import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;

import Fisica.Toner.DAOToner;
import Inteface.InterfaceGrilla;
import Inteface.InterfacePanel;
import Modelo.Toner;
import Views.Main.MainToner;

import javax.swing.JComboBox;

import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.filechooser.FileNameExtensionFilter;

public class TonerCampos extends JPanel implements InterfaceGrilla, InterfacePanel {
	private JTextField tfID;
	private JTextField tfCodigo;
	private JComboBox cbMarca, cbModelo;
	private JTextField tfMarca, tfModelo;
	private JTextField tfURLImg;
	
	public JLabel lblImg;

	/**
	 * Create the panel.
	 */
	public TonerCampos(int id) {
		setLayout(null);
		this.setVisible(true);
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(10, 11, 46, 14);
		add(lblId);
		
		tfID = new JTextField();
		tfID.setEditable(false);
		tfID.setBounds(66, 9, 53, 20);
		add(tfID);
		tfID.setColumns(10);
		
		JLabel lblMarca = new JLabel("Marca");
		lblMarca.setBounds(10, 42, 46, 14);
		add(lblMarca);
		
		JLabel lblModelo = new JLabel("Modelo");
		lblModelo.setBounds(10, 67, 46, 14);
		add(lblModelo);
		
		JLabel lblCodigo = new JLabel("Codigo");
		lblCodigo.setBounds(10, 92, 46, 14);
		add(lblCodigo);
		
		tfCodigo = new JTextField();
		tfCodigo.setBounds(66, 90, 86, 20);
		add(tfCodigo);
		tfCodigo.setColumns(10);
		
		cbMarca = new JComboBox();
		cbMarca.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				initModelos();
			}
		});
		cbMarca.setBounds(66, 37, 86, 20);
		add(cbMarca);
		
		cbModelo = new JComboBox();
		cbModelo.setBounds(66, 62, 86, 20);
		add(cbModelo);
		
		tfMarca = new JTextField();
		tfMarca.setEnabled(false);
		tfMarca.setEditable(false);
		tfMarca.setBounds(162, 37, 86, 20);
		add(tfMarca);
		tfMarca.setColumns(10);
		
		tfModelo = new JTextField();
		tfModelo.setEnabled(false);
		tfModelo.setEditable(false);
		tfModelo.setColumns(10);
		tfModelo.setBounds(162, 62, 86, 20);
		add(tfModelo);
		
		lblImg = new JLabel("");
		lblImg.setBounds(133, 155, 180, 180);
		add(lblImg);
		
		JButton btnBuscarImg = new JButton("Buscar");
		btnBuscarImg.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				buscarImg();
			}
		});
		btnBuscarImg.setBounds(258, 121, 85, 23);
		add(btnBuscarImg);
		
		JLabel lblUrlImg = new JLabel("URL Img.");
		lblUrlImg.setBounds(10, 124, 60, 14);
		add(lblUrlImg);
		
		tfURLImg = new JTextField();
		tfURLImg.setBounds(66, 122, 182, 20);
		add(tfURLImg);
		tfURLImg.setColumns(10);
		
		this.initMarcas();
		this.initModelos();
		
		this.init(id);

	}

	@Override
	public void guardar() {
		try {
			DAOToner th = new DAOToner();
			int id = -1;
			if(tfID.getText().length() > 0)
			 id = Integer.parseInt(tfID.getText());
			
			Toner t = (Toner) th.getById(Integer.valueOf(id));		
			
//			"ID", "Marca", "Modelo", "Codigo"
			String model = "", marca = "", codigo = "";
			if (cbModelo.getSelectedIndex() > 0)
				t.setModelo(cbModelo.getSelectedItem().toString());
			else
				if(tfModelo.getText().length()>0)
					t.setModelo(tfModelo.getText());
			
			if (cbMarca.getSelectedIndex() > 0)
				t.setMarca(cbMarca.getSelectedItem().toString());
			else 
				if(tfMarca.getText().length()>0)
					t.setMarca(tfMarca.getText());
			
			if (tfCodigo.getText().length() > 0)
				t.setCodigo(tfCodigo.getText());
			else t.setCodigo("");
			
			t.setUrlImg("");
			if(tfURLImg.getText().length()>0)
				t.setUrlImg(tfURLImg.getText());
						
			if(id == -1)
				th.insert(t);
			else th.update(t);
			
			this.cancelar();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}

	@Override
	public void cancelar() {
		tfID.setText("");
		cbMarca.setSelectedIndex(0);
		tfMarca.setText("");
		cbModelo.setSelectedIndex(0);
		tfModelo.setText("");
		tfCodigo.setText("");	
		tfURLImg.setText("");
		lblImg.removeAll();
	}

	public Object primero() {
		DAOToner dt = new DAOToner();
		return (Toner) dt.getFirst();		
	}

	public Object ultimo() {
		DAOToner dt = new DAOToner();
		return (Toner) dt.getLast();
	}

	public Object anterior() {
		DAOToner dt = new DAOToner();
		if (tfID.getText().length()>0)
			return (Toner) dt.getPre(Integer.parseInt(tfID.getText()));
		else return primero();
	}

	public Object posterior() {
		DAOToner dt = new DAOToner();
		if (tfID.getText().length()>0)
			return (Toner) dt.getPost(Integer.parseInt(tfID.getText()));
		else return primero();
	}

	@Override
	public int setprimero() {
		DAOToner dt = new DAOToner();
//		MainToner.idt = ((Toner) dt.getFirst()).getId();
		return -1;
		
	}

	@Override
	public int setultimo() {
		DAOToner dt = new DAOToner();
//		MainToner. =  ((Toner) dt.getLast()).getId();
		return -1;
		
	}

	@Override
	public int setanterior() {
		DAOToner dt = new DAOToner();
//		MainToner.tc = ((Toner) dt.getPre(Integer.parseInt(tfID.getText()))).getId();
		return -1;
		
	}

	@Override
	public int setposterior() {
		DAOToner dt = new DAOToner();
//		MainToner.tc = ((Toner) dt.getPost(Integer.parseInt(tfID.getText()))).getId();
		return -1;
	}
	
	@Override
	public boolean grilla() {
		return false;
	}

	@Override
	public boolean campos() {
		return true;
	}

	@Override
	public int getPanel() {
		return 0;
	}

	@Override
	public void init(int id) {	
		try {
			this.finNuevo();
			if(id > 0){
				DAOToner th = new DAOToner();
				Toner t = (Toner) th.getById(id);		
				tfID.setText(Integer.toString(t.getId()));
				cbMarca.setSelectedItem(t.getMarca());
				initModelos();
				cbModelo.setSelectedItem(t.getModelo());				
				
				tfCodigo.setText(t.getCodigo());
			}else{
				cancelar();
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	@Override
	public int getRowSelected() {
		return -1;
	}
	
	//CARGA LISTA DE MARCAS 
    public void initMarcas(){
        cbMarca.removeAllItems();
        List<Object>list = null;
        DAOToner dt = new DAOToner();
        Toner t;
        list = dt.listMarcas();
        cbMarca.addItem("");  
        for(Object o: list) {
        	t = (Toner) o;
            cbMarca.addItem(t.getMarca()); 
           } 
        }
    
  //CARGA LISTA DE MODELOS 
    public void initModelos(){
        List<Object>list = null;
        String marca; 
        DAOToner dt = new DAOToner();
        Toner t;
        if (cbMarca.getSelectedIndex() < 0){
                marca = "";             
        }else {
                marca = cbMarca.getSelectedItem().toString(); 
                list = dt.listMarca(marca);
	            cbModelo.removeAllItems();
	            cbModelo.addItem("");  
	            for(Object o: list) {
	                t = (Toner)o;
	                cbModelo.addItem(t.getModelo()); 
               } 
        }           
        }

	@Override
	public void nuevo() {
		boolean nuevo = !tfModelo.isEnabled();
		tfModelo.setEnabled(nuevo);
		tfModelo.setEditable(nuevo);
		tfMarca.setEnabled(nuevo);
		tfMarca.setEditable(nuevo);		
	}
	
	public void finNuevo() {
		tfModelo.setEnabled(false);
		tfModelo.setEditable(false);
		tfMarca.setEnabled(false);
		tfMarca.setEditable(false);		
	}
	
	public void buscarImg(){
		FileNameExtensionFilter filtro = new FileNameExtensionFilter("Formato de Archivo JPEG(*.JPG;*.JPEG,*.PNG", "jpg","jpeg","png");
		JFileChooser archivo = new JFileChooser();
		archivo.addChoosableFileFilter(filtro);
		archivo.setDialogTitle("Abrir archivo");
		File ruta = new File("C:/");
		archivo.setCurrentDirectory(ruta);
		
		int ventana = archivo.showOpenDialog(null);
		if(ventana == JFileChooser.APPROVE_OPTION){
			File file = archivo.getSelectedFile();
			tfURLImg.setText(String.valueOf(file)); //JTextField
			Image img = getToolkit().getImage(tfURLImg.getText());
			img = img.getScaledInstance(180, 180, Image.SCALE_DEFAULT);
			lblImg.setIcon(new ImageIcon(img));
			
		}
	}
}
